﻿using System.Net.Http.Json;
using System.Text.Json;

namespace WYJ.SDK.Web
{
    public class WYJHttpClient
    {
        private static readonly HttpClientHandler handler;
        private readonly HttpClient httpClient;

        static WYJHttpClient()
        {
            handler = new HttpClientHandler() { UseCookies = false };
        }

        public WYJHttpClient()
        {
            httpClient = new HttpClient(handler);
        }

        public async Task<T?> GetAsync<T>(string url) where T : class
        {

            await using Stream stream = await httpClient.GetStreamAsync(url);
            var result = await JsonSerializer.DeserializeAsync<T>(stream);
            return result;
        }

        public async Task<T?> PostJsonAsync<T>(string url, object postData) where T : new()
        {
            var result = await httpClient.PostAsJsonAsync(url, postData);
            return await result.Content.ReadFromJsonAsync<T>();
        }
    }
}